package com.yq;

import java.io.File;
import java.util.ArrayList;

public class FileManage implements Runnable
{
    private ArrayList<DFileList> list = new ArrayList<DFileList>();
    /** 搜索目录深度 */
    public static int MAX_DEAP = 0;
    
    private String path = null;
    private boolean sameExt = false;
    private static int currentIndex = 0;
    private static FileManage instance = null;
    
    public static final boolean search(String path, boolean sameExt) {
        instance = new FileManage();
        instance.list = new ArrayList<DFileList>();
        instance.path = path;
        instance.sameExt = sameExt;
        new Thread(instance).start();
        return true;
    }
    
    public static FileManage getInstance() {
    	return instance;
    }

    @Override
    public void run()
    {
        searchForlder(path, 0);
        chechDouble();
        DoubleFile.instance.searchFinish();
    }
    
    private void chechDouble()
    {
        for (int i = 0; i < list.size(); i++)
        {
            DFileList dfl = list.get(i);
            if (!dfl.isDouble()) {
                list.remove(i);
                i--;
            }
        }
    }

    public void add(File file) {
        long size = file.length();
        String lastName = DFileList.getLastName(file.getName());
        for (int i = 0; i < list.size(); i++)
        {
            DFileList dfl = list.get(i);
            if (dfl.fileSize == size) {
                if (sameExt && !lastName.endsWith(dfl.lastName)) {
                	continue;
                }
                dfl.add(file);
                return;
            }
        }
        DFileList newFileList = new DFileList(size, file);
        list.add(newFileList);
    }
    
    public int size() {
        return list.size();
    }
    
    public int getCurrentIndex() {
        return currentIndex;
    }
    
    public DFileList getCurrent() {
        if (list.size() == 0) {
            return null;
        }
        if (currentIndex < 0 || currentIndex >= list.size()) {
            currentIndex = 0;
        }
        return list.get(currentIndex);
    }
    
    public DFileList getPrev() {
        if (list.size() == 0) {
            return null;
        }
        if (currentIndex - 1 < 0) {
            currentIndex = list.size() - 1;
        } else {
            currentIndex--;
        }
        return list.get(currentIndex);
    }
    
    public DFileList getNext() {
        if (list.size() == 0) {
            return null;
        }
        if (currentIndex + 1 < 0 || currentIndex + 1 >= list.size()) {
            currentIndex = 0;
        } else {
            currentIndex++;
        }
        return list.get(currentIndex);
    }
    
    public void remove(DFileList fileList){
        list.remove(fileList);
    }
    
    public void removeAll(){
    	int count = 0;
    	while (list.size() > 0) {
    		DFileList dfl = list.get(0);
    		count += dfl.deleteAllFile();
            list.remove(0);
    	}
    	DoubleFile.instance.deleteUpdate();
        DoubleFile.instance.showMessage("删除文件" + count + "个");
    }
    
    public void searchForlder(String path, int deap) {
        if (MAX_DEAP > 0 && deap > MAX_DEAP) {
            return;
        }
        File file = new File(path);
        if (!file.exists()) {
            return;
        }
        if (file.isFile()) {
            add(file);
        } else if (file.isDirectory()) {
            File[] fileList = file.listFiles();
            if (fileList == null) {
                return;
            }
            for (int i = 0; i < fileList.length; i++)
            {
                searchForlder(fileList[i].getPath(), deap + 1);
            }
        }
    }
    
    public static final String arrayToString(String[] arr, String spt) {
        if (arr == null || arr.length == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < arr.length; i++)
        {
            sb.append(arr[i] + spt);
        }
        return sb.toString();
    }
    
}
